/**
 * Copyright 2000 self.com.au
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. (http://www.apache.org/licenses/LICENSE-2.0)
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed 
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for 
 * the specific language governing permissions and limitations under the License.
 *
 * <dhamma-message src="Atisha (11th century Tibetan Buddhist master)">
 * 	The greatest achievement is selflessness. The greatest worth is self-mastery.
 * 	The greatest quality is seeking to serve others. The greatest precept is continual awareness.
 * 	The greatest medicine is the emptiness of everything. The greatest action is not conforming with the worlds ways.
 * 	The greatest magic is transmuting the passions. The greatest generosity is non-attachment.
 * 	The greatest goodness is a peaceful mind. The greatest patience is humility.
 * 	The greatest effort is not concerned with results. The greatest meditation is a mind that lets go.
 * 	The greatest wisdom is seeing through appearances. 
 * </dhamma-message>
 *
 * @author Ashin Wimalajeewa
 */
package self.amigo.tool.sphinx.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

import self.amigo.module.IModule;

public class Splash extends JFrame {
	
	/**
	 * If the internal state of this class ever changes in such a way that it can't be defaulted,
	 * then the {@link #serialVersionUID} should be incremented to ensure serialized instances cleanly fail.  
	 */
	private static final long serialVersionUID = 1;
	
  final static public String APP_LOGO = "/self/amigo/images/sphinx.gif";
  BorderLayout borderLayout1 = new BorderLayout();
  JTabbedPane aboutPanes = new JTabbedPane();
  JPanel aboutPanel = new JPanel();
  JPanel creditsPanel = new JPanel();
  BorderLayout borderLayout2 = new BorderLayout();
  JScrollPane creditsScroller = new JScrollPane();
  BorderLayout borderLayout3 = new BorderLayout();
  JTable creditsTable = new JTable();

  URL imgURL;
  ImageIcon splashImage;
  JLabel splashButton;

  private TableModel creditsDataSource = new TableModel();
  private boolean showCredits;

  public Splash() {
    this( null );
  }

  public Splash( java.util.List modules ) {
    showCredits = modules != null;
    imgURL = Splash.class.getResource( APP_LOGO );
    try {
      splashImage = new ImageIcon( imgURL );
    } catch ( Exception err ) {
      System.out.println("missed Application Logo");
    }
    splashButton = new JLabel( splashImage );

    initImage();
    initCredits( modules );
    jbInit();
    pack();
  }

  private void jbInit() {
    Container contentPane = this.getContentPane();
    contentPane.setLayout(borderLayout1);
    this.setResizable(false);
    this.setTitle("Sphinx: Agile Modeling Editor");
    aboutPanel.setLayout(borderLayout2);
    aboutPanel.setBackground(Color.white);
    contentPane.add(aboutPanes, BorderLayout.CENTER);
    aboutPanes.add(aboutPanel, "About");
    aboutPanel.add(splashButton, BorderLayout.CENTER);

    if ( showCredits ) {
    aboutPanes.add(creditsPanel, "Credits");
    creditsPanel.add(creditsScroller, BorderLayout.CENTER);
    creditsScroller.getViewport().add(creditsTable, null);
    }
  }

  private void initImage() {
    if (splashImage != null) {
      int imgWidth = splashImage.getIconWidth();
      int imgHeight = splashImage.getIconHeight();
      Dimension scrSize = Toolkit.getDefaultToolkit().getScreenSize();
      setLocation(scrSize.width/2 - imgWidth/2,
		       scrSize.height/2 - imgHeight/2);
    }
  }

  private void initCredits( java.util.List modules ) {
    creditsDataSource.setSource( modules );
    creditsTable.setModel( creditsDataSource );
  }

  private class TableModel extends AbstractTableModel {
  	
	/**
	 * If the internal state of this class ever changes in such a way that it can't be defaulted,
	 * then the {@link #serialVersionUID} should be incremented to ensure serialized instances cleanly fail.  
	 */
	private static final long serialVersionUID = 1;
	
    private java.util.List modules;

    public void setSource( java.util.List mods ) {
      modules = mods;
    }

    public String getColumnName(int column) {
      switch ( column ) {
        case 0: return "Module name";
        case 1: return "Vendor copyright";
        case 2: return "Credits";
      }
      return null;
    }

    public int getColumnCount() { return 3; }
    public int getRowCount() {
    	if ( modules == null )
    		return 0;
    	return modules.size(); 
    }

    public Object getValueAt(int row, int col) {
      IModule mod = (IModule) modules.get( row );
      switch ( col ) {
        case 0: return mod.getName();
        case 1: return mod.getVendor();
        case 2: return mod.getCredits();
      }
      return null;
    }
 }
}
